/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.pig4cloud.pigx.user.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.common.log.annotation.SysLog;
import com.pig4cloud.pigx.user.dto.AppRechargeRecordPageDTO;
import com.pig4cloud.pigx.user.dto.RechargeRecordPageDTO;
import com.pig4cloud.pigx.user.dto.UpdateCapitalAccountDTO;
import com.pig4cloud.pigx.user.entity.RechargeRecord;
import com.pig4cloud.pigx.user.service.RechargeRecordService;
import com.pig4cloud.pigx.user.vo.AdminPageDemoVO;
import com.pig4cloud.pigx.user.vo.AppRechargeRecordPageVO;
import com.pig4cloud.pigx.user.vo.RechargeRecordPageVO;
import org.springframework.security.access.prepost.PreAuthorize;
import com.pig4cloud.pigx.common.excel.annotation.ResponseExcel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 充值记录
 *
 * @author pigx code generator
 * @date 2022-11-21 16:43:06
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/recharge_record" )
@Api(value = "recharge_record", tags = "充值记录管理")
public class RechargeRecordController {

    private final  RechargeRecordService rechargeRecordService;

    /**
     * 分页查询
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @PostMapping("/page" )
	@PreAuthorize("@pms.hasPermission('user_rechargerecord_view')" )
	public R<AdminPageDemoVO<RechargeRecordPageVO>> getRechargeRecordPage(@RequestBody RechargeRecordPageDTO dto) {
		return rechargeRecordService.voPage(dto);
    }

	/**
	 * 人工充值
	 * @param dto dto
	 * @return r
	 */
	@PostMapping("/user/recharge")
	public R<String> userRecharge(@RequestBody UpdateCapitalAccountDTO dto) {
		return rechargeRecordService.userRecharge(dto);
	}

	/**
	 * 获取 dApp 用户充值记录 分页
	 * @param dto dto
	 * @return r
	 */
	@PostMapping("/app/page")
	public R<Page<AppRechargeRecordPageVO>> getListByApp(@RequestBody AppRechargeRecordPageDTO dto) {
		return rechargeRecordService.getListByApp(dto);
	}

    /**
     * 通过id查询充值记录
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('user_rechargerecord_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(rechargeRecordService.getById(id));
    }

    /**
     * 新增充值记录
     * @param rechargeRecord 充值记录
     * @return R
     */
    @ApiOperation(value = "新增充值记录", notes = "新增充值记录")
    @SysLog("新增充值记录" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('user_rechargerecord_add')" )
    public R save(@RequestBody RechargeRecord rechargeRecord) {
        return R.ok(rechargeRecordService.save(rechargeRecord));
    }

    /**
     * 修改充值记录
     * @param rechargeRecord 充值记录
     * @return R
     */
    @ApiOperation(value = "修改充值记录", notes = "修改充值记录")
    @SysLog("修改充值记录" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('user_rechargerecord_edit')" )
    public R updateById(@RequestBody RechargeRecord rechargeRecord) {
        return R.ok(rechargeRecordService.updateById(rechargeRecord));
    }

    /**
     * 通过id删除充值记录
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除充值记录", notes = "通过id删除充值记录")
    @SysLog("通过id删除充值记录" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('user_rechargerecord_del')" )
    public R removeById(@PathVariable Long id) {
        return R.ok(rechargeRecordService.removeById(id));
    }


    /**
     * 导出excel 表格
     * @param rechargeRecord 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('user_rechargerecord_export')" )
    public List<RechargeRecord> export(RechargeRecord rechargeRecord) {
        return rechargeRecordService.list(Wrappers.query(rechargeRecord));
    }
}
